﻿using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.Scalesetting
{
    /// <summary>
    /// 活畜称重数据加载
    /// </summary>
    public class LiveAnimalWeightListHander : IRequestHandler<LiveAnimalWeightListCommand, Resqults<LiveAnimalWeighingListDTO>>
    {
        private readonly IBateRepository<LiveAnimalweighing> live;
        private readonly IBateRepository<Approach> approach;
        private readonly IBateRepository<SlaughterCircle> slaughterCircle;

        public LiveAnimalWeightListHander(IBateRepository<LiveAnimalweighing> live, IBateRepository<Approach> approach, IBateRepository<SlaughterCircle> slaughterCircle)
        {
            this.live = live;
            this.approach = approach;
            this.slaughterCircle = slaughterCircle;
        }

        public async Task<Resqults<LiveAnimalWeighingListDTO>> Handle(LiveAnimalWeightListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<LiveAnimalWeighingListDTO> res = new Resqults<LiveAnimalWeighingListDTO>();
                var list = (from a in live.GetAll()
                           join b in approach.GetAll() on a.ApproachNo equals b.ApproachNo
                           join c in slaughterCircle.GetAll() on a.SlaughterCircleId equals c.SlaughterCircledId
                           where a.State == 0
                           select new LiveAnimalWeighingListDTO()
                           {
                               LiveAnimalweighingId = a.LiveAnimalweighingId,
                               Suppliers=b.Suppliers,
                               ApproachNo=b.ApproachNo,
                               ApproachBatch=b.ApproachBatch,
                               ShoppingDesignationid=b.ShoppingDesignationid,
                               WeightNum = a.WeightNum,
                               SlaughterCircleName = c.SlaughterCircleName,
                               GrossWeight = a.GrossWeight,
                               Tare= a.Tare,
                               NetWeight = a.NetWeight,
                               AverageWeight = a.AverageWeight,
                               Deductions = a.Deductions,
                               Remark = a.Remark,
                               WeighTime=a.WeighTime    
                           }).ToList();
                res.Date = list;
                return res;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
